﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 药品信息管理
{
    public partial class YP_YXQCX : Form
    {
        SqlDataAdapter da = new SqlDataAdapter();//实例化sqldataadpte

        string glfs;//过滤方式

        string glnr;//过滤内容

        public YP_YXQCX()
        {
            InitializeComponent();
        }

        private void YXQ_show()
        {
            if (rb_all.Checked == true)//全部
                glfs = "";
            if (rb_gq.Checked == true)//过期
                glfs = " AND (select convert(varchar(10),(select convert(datetime,YXRQ-2)),120))<=(select convert(varchar(10),getdate(),120))";
            if (rb_yx.Checked == true)//有效
                glfs = " AND (select convert(varchar(10),(select convert(datetime,YXRQ-2)),120))>(select convert(varchar(10),getdate(),120))";
            
            if(rb_date.Checked==true && dateTimeInput1.Value.Year!=0001)//选定失效日期
                glfs = " AND (select convert(varchar(10),(select convert(datetime,YXRQ-2)),120))<=(select convert(varchar(10),(select Convert(DateTime,'"+dateTimeInput1.Value+"')),120))";

            if (rb_dif.Checked == true && integerInput1.Text.ToString() != "")
                glfs = " AND YXRQ<=(select convert(int,(select Convert(DateTime,'" + DateTime.Today + "')))+2+" + integerInput1.Value.ToString() + ")  AND YXRQ>(select convert(int,(select Convert(DateTime,'" + DateTime.Today + "')))+2)";

           
            try
            {
                using (SqlConnection con = new SqlConnection(SQL.connectDB))
                {
                    DataTable dt_bmdw = new DataTable();
                    string sqlyj="SELECT DH,BM 编码,MC 名称,GG 规格,SL 数量,DJ 单价,(SL*DJ) AS 总额,(select convert(varchar(10),(select convert(datetime,YXRQ-2)),120)) as 有效日期 FROM YK_RKD1 LEFT JOIN ZD_YP ON YPBM=BM  WHERE 1=1 AND (BM LIKE'%" + textBoxX1.Text.ToString() + "%' OR DH LIKE'%" + textBoxX1.Text.ToString() + "%' OR MC LIKE'%" + textBoxX1.Text.ToString() + "%')" + glfs;
                    SqlCommand BMDW = new SqlCommand(sqlyj, con);//sql语句
                    da.SelectCommand = BMDW;//设置为已实例化SqlDataAdapter的查询命令
                    da.Fill(dt_bmdw);
                    dataGridViewX1.DataSource = dt_bmdw;
                    dataGridViewX1.Columns[0].Visible = false;

                }
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.ToString());
            }
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void rb_all_CheckedChanged(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void rb_gq_CheckedChanged(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void rb_yx_CheckedChanged(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void rb_date_CheckedChanged(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void rb_dif_CheckedChanged(object sender, EventArgs e)
        {
            YXQ_show();
        }

        private void buttonItem2_Click(object sender, EventArgs e)
        {
            dgVprint1.Print(dataGridViewX1, true);
        }
    }
}